package com.example.wangzh.developkit.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.example.wangzh.developkit.base.MyApplication;
import com.wzh.advancelog.log.WzhLog;

import java.lang.reflect.Method;


@SuppressWarnings({"unused", "RedundantSuppression", "deprecation", "JavaReflectionMemberAccess"})
public class UIUtils {

    public static final String TAG = UIUtils.class.getSimpleName();
    public static Toast mToast;

    /**
     * 得到上下文
     *
     * @return
     */
    public static Context getContext() {
        return MyApplication.getInstance().getApplicationContext();
    }

    /**
     * 优化的弹出弹窗方法
     *
     * @param msg
     */
    public static void showToast(String msg) {
        showToast(msg, Toast.LENGTH_SHORT);
    }


    /**
     * 优化的弹出弹窗方法
     *
     * @param msg
     * @param duration
     */
    @SuppressLint("ShowToast")
    public static void showToast(String msg, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(getContext(), "", duration);
        }
        mToast.setText(msg);
        mToast.show();
    }

    /**
     * 获取屏幕高度
     *
     * @return
     */
    public static int getScreenHeight() {
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        return dm.heightPixels;
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    public static int getScreenWidth() {
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    /**
     * 获取设备的dpi
     *
     * @param mContext
     * @return
     */
    public static int getDpi(Activity mContext) {
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        return dm.densityDpi;
    }

    /**
     * dip-->px
     */
    public static int dip2Px(float dip) {
        // px/dip = density;
        // density = dpi/160
        // 320*480 density = 1 1px = 1dp
        // 1280*720 density = 2 2px = 1dp

        float density = getResource().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f);
    }

    /**
     * px-->dip
     */
    public static float px2Dip(int px) {
        float density = getResource().getDisplayMetrics().density;
        return px / density;
    }

    /**
     * sp-->px
     */
    public static int sp2px(float sp) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                getResource().getDisplayMetrics()) + 0.5f);
    }

    public static void logMetrics() {
        logMetrics(null);
    }

    public static void logMetrics(String tag) {
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        if (TextUtils.isEmpty(tag)) {
            WzhLog.i(TAG, dm.toString());
        } else {
            WzhLog.i(tag, dm.toString());
        }
    }

    /**
     * 获取控件的真实高度
     *
     * @param view
     * @return
     */
    public static int getRealHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredHeight();
    }

    /**
     * 获取控件的真实宽度
     *
     * @param view
     * @return
     */
    public static int getRealWidth(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredWidth();
    }

    /**
     * 得到resources对象
     *
     * @return
     */
    public static Resources getResource() {
        return getContext().getResources();
    }

    /**
     * 得到string.xml中的字符串
     *
     * @param resId
     * @return
     */
    public static String getString(int resId) {
        return getResource().getString(resId);
    }

    /**
     * 得到string.xml中的字符串，带点位符
     *
     * @return
     */
    public static String getString(int id, Object... formatArgs) {
        return getResource().getString(id, formatArgs);
    }

    /**
     * 得到string.xml中和字符串数组
     *
     * @param resId
     * @return
     */
    public static String[] getStringArr(int resId) {
        return getResource().getStringArray(resId);
    }

    /**
     * 得到colors.xml中的颜色
     *
     * @param colorId
     * @return
     */
    public static int getColor(int colorId) {
        return getResource().getColor(colorId);
    }


    /**
     * 隐藏软键盘
     *
     * @param context
     * @param view
     */
    public static void hideSoftInput(Context context, View view) {
        InputMethodManager immHide = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); // 隐藏软键盘
        immHide.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 显示键盘
     *
     * @param context
     * @param view
     */
    public static void showSoftInput(Context context, View view) {
        InputMethodManager manager = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE));
        if (manager != null) manager.showSoftInput(view, 0);

    }

    /**
     * 上拉状态栏，关闭通知详情页
     *
     * @param context
     */
    public static void collapseStatusBar(Context context) {
        @SuppressLint("WrongConstant") Object service = context.getSystemService("statusbar");
        if (null == service)
            return;
        try {
            Class<?> clazz = Class.forName("android.app.StatusBarManager");
            int sdkVersion = android.os.Build.VERSION.SDK_INT;
            Method collapse;
            if (sdkVersion <= 16) {
                collapse = clazz.getMethod("collapse");
            } else {
                collapse = clazz.getMethod("collapsePanels");
            }
            collapse.setAccessible(true);
            collapse.invoke(service);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}